<template>
  <div class="app-container">
    <!-- 项目立项管理修改新增弹窗 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="180px">
        <div class="parent_dom">
          <div class="left" v-for="(item,index) in form_col" :key="index">
            <el-form-item :label="item.title" :prop="item.prop">
              <el-input v-if="item.type === 'input'" v-model="form[item.prop]" :placeholder="item.placeholder" size="mini" />
              <el-select v-if="item.type === 'select'" v-model="form[item.prop]" :placeholder="item.placeholder" clearable size="mini" style="width:100%">
                <el-option v-for="dict in item.options" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
              <el-date-picker v-if="item.type === 'date'" v-model="form[item.prop]" type="date" size="mini" :placeholder="item.placeholder" value-format="yyyy-MM-dd" style="width:100%">
              </el-date-picker>
            </el-form-item>
          </div>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel" size="mini">取 消</el-button>
        <el-button type="primary" @click="submitForm" size="mini">{{btn}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {  getProjects, addProjects, updateProjects } from "@/api/business/project/projects";
export default {
  dicts: ['pm_status_type'],
  data() {
    return {
      title: '新增项目立项信息',
      open: false,
      form: {
        pName: null,
        pSgdw: null,
        pStatus: null,
        startDate: null,
        endDate: null,
        pXmjl: null,
        pLxfs: null,
        pMoney: null,
      },
      form_col: [{ title: '项目名称', prop: 'pName', type: 'input', placeholder: '请输入项目名称', options: [], show: true, disabled: false },
      { title: '施工单位', prop: 'pSgdw', type: 'input', placeholder: '请输入施工单位', options: [], show: true, disabled: false },
      { title: '项目状态', prop: 'pStatus', type: 'select', placeholder: '请选择项目状态', options: [], show: true, disabled: false },
      // { title: '联系方式', prop: 'name', type: 'input', placeholder: '请选择', options: [], show: true, disabled: false },
      { title: '开始时间', prop: 'startDate', type: 'date', placeholder: '选择开始时间', options: [], show: true, disabled: false },
      { title: '结束时间', prop: 'endDate', type: 'date', placeholder: '选择结束时间', options: [], show: true, disabled: false },
      { title: '施工单位负责人', prop: 'pXmjl', type: 'input', placeholder: '请输入施工单位负责人', options: [], show: true, disabled: false },
      { title: '施工单位负责人联系方式', prop: 'pLxfs', type: 'input', placeholder: '请输入施工单位负责人联系方式', options: [], show: true, disabled: false },
      { title: '费用', prop: 'pMoney', type: 'input', placeholder: '请输入费用', options: [], show: true, disabled: false },
      ],
      rules: {},
      btn: '新增'
    }
  },
  mounted() {
    this.getoptions()
  },
  methods: {
    /**获取相对应字典值 */
    getoptions() {
      this.form_col.forEach(element => {
        element.prop === 'pStatus' ? element.options = this.dict.type.pm_status_type : []
      });
    },
    /**表单重置 */
    reset() {
      this.form = {
        pName: null,
        pSgdw: null,
        pStatus: null,
        startDate: null,
        endDate: null,
        pXmjl: null,
        pLxfs: null,
        pMoney: null,
      };
      this.resetForm("form");
    },
    /**组件init方法 */
    init(row) {
      this.open = true;
      this.reset()
      if (row) {
        this.title = '修改工程管理系统-工程项目信息'
        this.btn = '修改'
        this.getProjectsDetail(row)
      } else {
        this.title = '添加工程管理系统-工程项目信息'
        this.btn = '新增  '
      }
    },
    /** 获取详情 */
    getProjectsDetail(row) {
      const id = row.id || this.ids
      getProjects(id).then(response => {
        this.form = response.data;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProjects(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.$parent.getList()
            });
          } else {
            addProjects(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.$parent.getList()
            });
          }
        }
      });
    },
    /**取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    }
  }
}
</script>

<style lang="scss" scoped>
.parent_dom {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  .left {
    width: 50%;
  }
}
</style>